﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

public partial class Users_RescheduleTicket : System.Web.UI.Page
{
    Ticket OldTicket;
    List<Flight> FlightList;
    TicketBAL _ticketBAL;
    FlightBAL _flightBAL;
    protected void Page_Load(object sender, EventArgs e)
    {
        string userid = Membership.GetUser().ProviderUserKey.ToString();
        int ticketid = 0;
        try
        {
            ticketid = Convert.ToInt32(Request.QueryString["id"]);
        }
        catch (Exception)
        {
            Response.Redirect("~/Users/TicketList.aspx");
        }

        _ticketBAL = new TicketBAL();
        _flightBAL = new FlightBAL();

        try
        {
            OldTicket = _ticketBAL.getByID(ticketid);
            OldTicket.TicketDetail = _ticketBAL.getTicketDetail(ticketid);
            OldTicket.Flight = _flightBAL.getFlightbyID(OldTicket.Flight.FlightID);
        }
        catch (Exception)
        {
            Response.Redirect("~/Users/TicketList.aspx");
        }


        if (OldTicket.Status == "Buy" && OldTicket.UserID == userid && DateTime.Now.AddDays(21) < OldTicket.Flight.DepartTime)
        {
            int month = Convert.ToInt32(ddlMonth.SelectedValue);
            FlightList = _flightBAL.findSimilarFlight(OldTicket, month);
            for (int i = 0; i < FlightList.Count; i++)
            {
                Flight flight = FlightList[i];

                if (flight.FlightID == OldTicket.Flight.FlightID)
                {
                    FlightList.Remove(flight);
                    i--;
                }
            }
            if (!Page.IsPostBack)
            {
                List<Flight> temp = new List<Flight>();
                temp.Add(OldTicket.Flight);
                DataList1.DataSource = temp;
                DataList1.DataBind();



                GridView1.DataSource = FlightList;
                GridView1.DataBind();
                if (FlightList.Count == 0)
                {
                    Button1.Enabled = false;
                }
                else
                {
                    foreach (Flight flight in FlightList)
                    {
                        ListItem item = new ListItem(flight.FlightID.ToString());
                        ddlFlight.Items.Add(item);
                    }
                    ddlFlight.SelectedIndex = 0;
                    int numofPass = OldTicket.TicketDetail.Count;
                    if (FlightList[0].AvailBussSeat > numofPass)
                    {
                        ListItem item = new ListItem("Bussiness");
                        ddlSeat.Items.Add(item);
                    }
                    if (FlightList[0].AvailFirstSeat > numofPass)
                    {
                        ListItem item = new ListItem("First");
                        ddlSeat.Items.Add(item);
                    }
                    if (FlightList[0].AvailClubSeat > numofPass)
                    {
                        ListItem item = new ListItem("Club");
                        ddlSeat.Items.Add(item);
                    }
                    Button1.Enabled = true;
                }

            }
        }
        else
        {
            Button1.Enabled = false;
            lblWarn.Visible = true;
        }
    }

    protected void ddlMonth_SelectedIndexChanged(object sender, EventArgs e)
    {
        List<Flight> temp = new List<Flight>();
        temp.Add(OldTicket.Flight);
        DataList1.DataSource = temp;
        DataList1.DataBind();

        int month = Convert.ToInt32(ddlMonth.SelectedValue);
        FlightList = _flightBAL.findSimilarFlight(OldTicket, month);
        for (int i = 0; i < FlightList.Count; i++)
        {
            Flight flight = FlightList[i];

            if (flight.FlightID == OldTicket.Flight.FlightID)
            {
                FlightList.Remove(flight);
                i--;
            }
        }
        GridView1.DataSource = FlightList;
        GridView1.DataBind();

        ddlFlight.Items.Clear();
        ddlSeat.Items.Clear();

        if (FlightList.Count == 0)
        {
            Button1.Enabled = false;
        }
        else
        {
            foreach (Flight flight in FlightList)
            {
                ListItem item = new ListItem(flight.FlightID.ToString());
                ddlFlight.Items.Add(item);
            }
            ddlFlight.SelectedIndex = 0;
            int numofPass = OldTicket.TicketDetail.Count;

            if (FlightList[0].AvailBussSeat > numofPass)
            {
                ListItem item = new ListItem("Bussiness");
                ddlSeat.Items.Add(item);
            }
            if (FlightList[0].AvailFirstSeat > numofPass)
            {
                ListItem item = new ListItem("First");
                ddlSeat.Items.Add(item);
            }
            if (FlightList[0].AvailClubSeat > numofPass)
            {
                ListItem item = new ListItem("Club");
                ddlSeat.Items.Add(item);
            }
            Button1.Enabled = true;
        }
    }

    protected void ddlFlight_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlFlight.Items.Count > 0)
        {
            int index = ddlFlight.SelectedIndex;
            int numofPass = OldTicket.TicketDetail.Count;
            ddlSeat.Items.Clear();
            if (FlightList[index].AvailBussSeat > numofPass)
            {
                ListItem item = new ListItem("Bussiness");
                ddlSeat.Items.Add(item);
            }
            if (FlightList[index].AvailFirstSeat > numofPass)
            {
                ListItem item = new ListItem("First");
                ddlSeat.Items.Add(item);
            }
            if (FlightList[index].AvailClubSeat > numofPass)
            {
                ListItem item = new ListItem("Club");
                ddlSeat.Items.Add(item);
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Session.Clear();
        Session["OldTicket"] = OldTicket;
        int index = ddlFlight.SelectedIndex;
        string seatClass= ddlSeat.SelectedValue;

        Flight flight = FlightList[index];
        flight.Route = OldTicket.Flight.Route;
        Ticket newticket = new Ticket();
        newticket.Flight = flight;

        foreach (TicketDetail detail in OldTicket.TicketDetail)
        {
            TicketDetail x = new TicketDetail();
            x.Passenger = detail.Passenger;
            x.SeatClass = seatClass;
            newticket.TicketDetail.Add(x);
        }

        Session["NewTicket"] = newticket;

        Response.Redirect("~/Users/RescheduleTicket1.aspx");
    }
}
